//
//  CollectionViewUtils.swift
//  novelReader
//
//  Created by Ink on 2019/9/12.
//  Copyright © 2019 ChineseAll. All rights reserved.
//

public protocol SupplementaryViewKindProtocol {
    static var kind: String { get }
}

extension SupplementaryViewKindProtocol {
    public static var kind: String { UICollectionView.elementKindSectionHeader }
}

extension UICollectionReusableView: SupplementaryViewKindProtocol {}

extension UICollectionView: RegisterCellProtocol {
    
    public typealias CType = UICollectionViewCell
    
    // MARK: - register
    public final func register<T: CType>(_ cellType: T.Type) {
        register(cellType, forCellWithReuseIdentifier: T.reuseIdentifier)
    }
    
    public final func registerNib<T: CType>(_ cellType: T.Type) {
        register(T.nib, forCellWithReuseIdentifier: T.reuseIdentifier)
    }
}

extension UICollectionView:RegisterHeaderFooterProtocol {
    
    public typealias HFType = UICollectionReusableView

    public func registerHeaderFooterView<T: HFType>(_ type: T.Type) {
        register(type, forSupplementaryViewOfKind: T.kind, withReuseIdentifier: T.reuseIdentifier)
    }
    
    public func registerHeaderFooterViewNib<T: HFType>(_ type: T.Type) {
        register(T.nib, forSupplementaryViewOfKind: T.kind, withReuseIdentifier: T.reuseIdentifier)
    }
    
    public func registerEmptyHeaderFooterView() {
        register(HFType.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: HFType.reuseIdentifier)
        register(HFType.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: HFType.reuseIdentifier)
    }
}

extension UICollectionView {
    
    // MARK: - dequeueReusable
    // use it ,must be registered
    public func dequeueReusableCell<T : CType>(for indexPath: IndexPath) -> T {
        dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath) as! T
    }
    
    public func dequeueReusableCell<T : CType>(for indexPath: IndexPath,type: T.Type) -> T {
        dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath) as! T
    }
    
    // MARK: - dequeueReusable
    // use it ,must be registered
    public func dequeueReusableSupplementaryView<T : HFType>(for indexPath: IndexPath) -> T {
        dequeueReusableSupplementaryView(ofKind: T.kind, withReuseIdentifier: T.reuseIdentifier, for: indexPath) as! T
    }
    
    public func dequeueEmptyHeaderFooterView(ofKind elementKind: String, for indexPath: IndexPath) -> HFType  {
        dequeueReusableSupplementaryView(ofKind: elementKind, withReuseIdentifier: HFType.reuseIdentifier, for: indexPath)
    }
}

public protocol CellSizeProtocol: ViewSizeProtocol {}

extension CellSizeProtocol {
    @_disfavoredOverload
    public static var viewSize : CGSize { UICollectionViewFlowLayout.automaticSize }
}

public protocol CollectionLayoutSizeProtocol: CellSizeProtocol {
    static var collectionLayoutSize : NSCollectionLayoutSize { get }
}

extension CollectionLayoutSizeProtocol {
    @_disfavoredOverload
    public static var collectionLayoutSize: NSCollectionLayoutSize {
        NSCollectionLayoutSize(widthDimension: .absolute(viewSize.width), heightDimension: .absolute(viewSize.height))
    }
}

extension UICollectionViewCell: CollectionLayoutSizeProtocol,CellSizeProtocol {}

open class CollectionSectionHeaderFooterEmptyView: UICollectionReusableView {
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class CollectionSectionHeaderEmptyView: CollectionSectionHeaderFooterEmptyView {
    public static var kind: String { UICollectionView.elementKindSectionHeader }
}

class CollectionSectionFooterEmptyView: CollectionSectionHeaderFooterEmptyView {
    
    public static var kind: String { UICollectionView.elementKindSectionFooter }
}
